<?php
// $Id$

/**
 * @file
 * World of Warcraft themeing for guild pages.
 */

/**
 *
 */
function template_preprocess_guild_roster_table__wow(&$variables) {
  $wow_roster_header = _wow_get_roster_header();
  $ts = tablesort_init($wow_roster_header);
  $header = '';
  $active = 0;
  
  foreach ($wow_roster_header as $cell) {
    // Manually set the active column, it is not done for us.
    if (is_array($cell) && isset($cell['field'])) {
      if ($cell['data'] == $ts['name']) {
        $variables['active'] = $active;
        $order = $ts['name'];
      }
    }
    
    // Theme the fancy sorting header columns.
    $cell = tablesort_header($cell, $wow_roster_header, $ts);
    $header .= _theme_table_cell($cell, TRUE);
    
    $active++;
  }
  
  $variables['header'] = $header;
  $variables['pager'] = theme('pager', NULL, $variables['players_per_page'], 0);
  $variables['inactive_filter'] = variable_get('guild_inactive_filter', FALSE);
  
  if (empty($variables['players'])) {
    $variables['players'] = array();
    return;
  }
  
  $classNames = wow_get_class_names();
  $raceNames = wow_get_race_names();
  $player_group = array();
  
  // Break the players into subgroups based on race id or class id depending
  // on the sort order. The database has already sorted the players by name.
  // If neither is selected, a default group key of 0 is used instead.
  foreach ($variables['players'] as $nid => $player) {
    $key = $order == t('Race') ? $player->raceId : ($order == t('Class') ? $player->classId : 0);
    
    $player_group[$key][$nid]->name = check_plain($player->name);
    $player_group[$key][$nid]->rankName = empty($player->rankName) ? '' : check_plain($player->rankName);
    
    $player_group[$key][$nid]->level = $player->level;
    $player_group[$key][$nid]->classId = $player->classId;
    $player_group[$key][$nid]->className = $classNames[$player->classId];
    $player_group[$key][$nid]->raceId = $player->raceId;
    $player_group[$key][$nid]->raceName = $raceNames[$player->raceId];
    $player_group[$key][$nid]->genderId = $player->genderId;
    
    if (user_access('access player content')) {
      $player_group[$key][$nid]->link = url('node/' . $nid);
    }
  }
  
  // Reset the players variable before appending data to it.
  $variables['players'] = array();
  
  if ($order != t('Race') && $order != t('Class')) {
    $variables['players'] = $player_group[0];
  }
  else {
    // Sort the language specific race or class text and use that to order
    // the player subgroups.
    $sort_function = ($ts['sort'] == 'desc') ? 'arsort' : 'asort';
    $sort_data = $order == t('Race') ? $raceNames : $classNames;
    $sort_function($sort_data);
    
    foreach ($sort_data as $id => $name) {
      // Note that the sorted text includes ALL races/classes. This is not a
      // problem for classes (at least not since TBC), but adding races of the
      // opposite faction causes problems.
      if (isset($player_group[$id])) {
        $variables['players'] = $variables['players'] + $player_group[$id];
      }
    }
  }
  
  // One last pass through to add zebra stripes. Resorting interferes with
  // processing this during the first loop.
  $row = 1;
  foreach ($variables['players'] as $nid => $player) {
    $variables['players'][$nid]->zebra = ($row % 2 == 0) ? 'even' : 'odd';
    $row++;
  }
}
